package main;
import goldengine.java.GOLDParser;
import goldengine.java.GPMessageConstants;
import goldengine.java.ParserException;
import goldengine.java.Reduction;
import goldengine.java.Token;

public class Parser implements GPMessageConstants {

	public static void main(String[] args) {
		String textToParse = args[0], compiledGrammar = args[1];
	
		goldengine.java.GOLDParser parser = new GOLDParser();

		try {
			parser.loadCompiledGrammar(compiledGrammar);
			parser.openFile(textToParse);
		} catch (ParserException parse) {
			System.out.println("**PARSER ERROR**\n" + parse.toString());
			System.exit(1);
		}

		boolean done = false;
		int response = -1;

		while (!done) {
			
			try {
				response = parser.parse();
			} catch (ParserException parse) {
				System.out.println("**PARSER ERROR**\n" + parse.toString());
				System.exit(1);
			}

			switch (response) {
			case gpMsgTokenRead:
				/*
				 * A token was read by the parser. The Token Object can be
				 * accessed through the CurrentToken() property:
				 * Parser.CurrentToken
				 */

				// ************************************** log file
				//System.out.println("gpMsgTokenRead");
				Token myTok = parser.currentToken();
				//System.out.println((String) myTok.getData());
				// ************************************** end log

				break;

			case gpMsgReduction:
				/*
				 * This message is returned when a rule was reduced by the parse
				 * engine. The CurrentReduction property is assigned a Reduction
				 * object containing the rule and its related tokens. You can
				 * reassign this property to your own customized class. If this
				 * is not the case, this message can be ignored and the
				 * Reduction object will be used to store the parse tree.
				 */

				// switch(Parser.CurrentReduction.TableIndex)
				// {
				// ...
				// }

				// Parser.Reduction = //Object you created to store the rule

				// ************************************** log file
				//System.out.println("gpMsgReduction");
				Reduction myRed = parser.currentReduction();
				//System.out.println(myRed.getParentRule().getText());
				// ************************************** end log

				break;

			case gpMsgAccept:
				/* The program was accepted by the parsing engine */

				// ************************************** log file
				System.out.println("File " + textToParse + " successfully parsed.");
				// ************************************** end log

				done = true;

				break;

			case gpMsgLexicalError:
				/*
				 * Place code here to handle a illegal or unrecognized token To
				 * recover, pop the token from the stack: Parser.PopInputToken
				 */

				// ************************************** log file
				System.out.println("LexicalError."); done = true;
				// ************************************** end log

				parser.popInputToken();

				break;

			case gpMsgNotLoadedError:
				/* Load the Compiled Grammar Table file first. */

				// ************************************** log file
				System.out.println("NotLoadedError");
				// ************************************** end log

				done = true;

				break;

			case gpMsgSyntaxError:
				/*
				 * This is a syntax error: the source has produced a token that
				 * was not expected by the LALR State Machine. The expected
				 * tokens are stored into the Tokens() list. To recover, push
				 * one of the expected tokens onto the parser's input queue (the
				 * first in this case): You should limit the number of times
				 * this type of recovery can take place.
				 */

				done = true;

				Token theTok = parser.currentToken();
				System.out.println("Token not expected: "
						+ (String) theTok.getData());

				// ************************************** log file
				System.out.println("Syntax Error at line " + parser.currentLineNumber() + ".");
				// ************************************** end log

				break;

			case gpMsgCommentError:
				/*
				 * The end of the input was reached while reading a comment.
				 * This is caused by a comment that was not terminated
				 */

				// ************************************** log file
				System.out.println("CommentError");
				// ************************************** end log

				done = true;

				break;

			case gpMsgInternalError:
				/*
				 * Something horrid happened inside the parser. You cannot
				 * recover
				 */

				// ************************************** log file
				System.out.println("InternalError");
				// ************************************** end log

				done = true;

				break;
			default: System.out.println(response); break;
			}
		}
		try {
			parser.closeFile();
		} catch (ParserException parse) {
			System.out.println("**PARSER ERROR**\n" + parse.toString());
			System.exit(1);
		}
	}
}